package com.qgs.rabbitmqapi.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @Author: 蔡翔
 * @Date: 2019/7/15 13:29
 * @Version 1.0
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //1.创建一个ConnectionFactory,并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.0.229");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        //3.通过connection创建一个Channel
        Channel channel = connection.createChannel();

        //4.通过Channel发送数据  (生产者可以直接通过channel发送消息)
        //channel.basicPublish(exchange,routingkey,props,body); props是对body的修饰，body是发送的实体,body是字节的二维数组
        for(int i=0;i<5;i++){
            String message = "hello rabbitmq" + i;
            channel.basicPublish("","test01",null,message.getBytes());
        }

        //5.记得要关闭相对应的连接。 由小到大关闭，channel关了再关connection。
        channel.close();
        connection.close();

    }
}
